
#include "pspvideo.h"
#include "pspdialogs.h"

void pspShowErrorDialog(const int error, int language)
{
    int done = 0;
    pspUtilityMsgDialogParams dialog;

    memset(&dialog, 0, sizeof(dialog));
    dialog.base.size = sizeof(dialog);
    if(language == DIALOG_LANGUAGE_AUTO)
    {
        sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_LANGUAGE, &dialog.base.language);
    }
    else
    {
        dialog.base.language = language;
    }
    sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_UNKNOWN, &dialog.base.buttonSwap);
    dialog.base.graphicsThread = 0x11;
    dialog.base.accessThread = 0x13;
    dialog.base.fontThread = 0x12;
    dialog.base.soundThread = 0x10;
    dialog.unknown = 0;
    dialog.mode = PSP_UTILITY_MSGDIALOG_MODE_ERROR;
    dialog.errorValue = error;
    dialog.options = PSP_UTILITY_MSGDIALOG_OPTION_ERROR;
    dialog.buttonPressed = PSP_UTILITY_MSGDIALOG_RESULT_UNKNOWN1;
    sceUtilityMsgDialogInitStart(&dialog);
    while(!done)
    {
        minimalRender();
        switch(sceUtilityMsgDialogGetStatus())
        {
            case PSP_UTILITY_DIALOG_NONE:
                break;
            case PSP_UTILITY_DIALOG_INIT :
                break;
            case PSP_UTILITY_DIALOG_VISIBLE:
                sceUtilityMsgDialogUpdate(2);
                break;
            case PSP_UTILITY_DIALOG_QUIT:
                sceUtilityMsgDialogShutdownStart();
                break;
            case PSP_UTILITY_DIALOG_FINISHED :
                done = 1;
                break;
        }
        videoFlipScreen(1);
    }
}

void pspShowMessageDialog(MESSAGE_HELPER *mh, int language)
{
    int done=0;
    pspUtilityMsgDialogParams dialog;

    memset(&dialog, 0, sizeof(dialog));
    dialog.base.size = sizeof(dialog);
    if(language == DIALOG_LANGUAGE_AUTO)
    {
        sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_LANGUAGE, &dialog.base.language);
    }
    else
    {
        dialog.base.language = language;
    }
    sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_UNKNOWN, &dialog.base.buttonSwap);
    dialog.base.graphicsThread = 0x11;
    dialog.base.accessThread = 0x13;
    dialog.base.fontThread = 0x12;
    dialog.base.soundThread = 0x10;
    dialog.unknown = 0;
    dialog.mode = PSP_UTILITY_MSGDIALOG_MODE_TEXT;
    dialog.errorValue = 0;
    strcpy(dialog.message, mh->message);
    dialog.options = mh->options;
    dialog.buttonPressed = PSP_UTILITY_MSGDIALOG_RESULT_UNKNOWN1;
    sceUtilityMsgDialogInitStart(&dialog);
    while(!done)
    {
        minimalRender();
        switch(sceUtilityMsgDialogGetStatus())
        {
            case PSP_UTILITY_DIALOG_NONE:
                break;
            case PSP_UTILITY_DIALOG_INIT :
                break;
            case PSP_UTILITY_DIALOG_VISIBLE:
                sceUtilityMsgDialogUpdate(2);
                break;
            case PSP_UTILITY_DIALOG_QUIT:
                sceUtilityMsgDialogShutdownStart();
                break;
            case PSP_UTILITY_DIALOG_FINISHED :
                done = 1;
                break;
        }
        videoFlipScreen(1);
    }
    mh->result = dialog.base.result;
    mh->buttonPressed = dialog.buttonPressed;
}

void pspShowOSK(OSK_HELPER *oskhelper, int language)
{
    int done=0;
    SceUtilityOskData data;
    SceUtilityOskParams osk;

    memset(&data, 0, sizeof(data));
    data.unk_00 = 1;           // 1=kanji
    data.unk_04 = 0;
    data.language = PSP_UTILITY_OSK_LANGUAGE_DEFAULT;
    data.unk_12 = 0;
    data.inputtype = PSP_UTILITY_OSK_INPUTTYPE_ALL;
    data.lines = oskhelper->lines;
    data.unk_24 = 0;
    data.desc = oskhelper->title;
    data.intext = oskhelper->pretext;
    data.outtextlength = oskhelper->textlength;
    data.outtextlimit = oskhelper->textlimit;
    data.outtext = oskhelper->text;
    memset(&osk, 0, sizeof(osk));
    osk.base.size = sizeof(osk);
    if(language == DIALOG_LANGUAGE_AUTO)
    {
        sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_LANGUAGE, &osk.base.language);
    }
    else
    {
        osk.base.language = language;
    }
    sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_UNKNOWN, &osk.base.buttonSwap);
    osk.base.graphicsThread = 0x11;
    osk.base.accessThread = 0x13;
    osk.base.fontThread = 0x12;
    osk.base.soundThread = 0x10;
    osk.datacount = 1;
    osk.data = &data;
    sceUtilityOskInitStart(&osk);
    while(!done)
    {
        minimalRender();
        switch(sceUtilityOskGetStatus())
        {
            case PSP_UTILITY_DIALOG_NONE :
                break;
            case PSP_UTILITY_DIALOG_INIT :
                break;
            case PSP_UTILITY_DIALOG_VISIBLE :
                sceUtilityOskUpdate(1);
                break;
            case PSP_UTILITY_DIALOG_QUIT :
                sceUtilityOskShutdownStart();
                break;
            case PSP_UTILITY_DIALOG_FINISHED :
                done = 1;
                break;
        }
        sceKernelDelayThread(10*1000);
        videoFlipScreen(1);
    }
}
